package com.xm.apd.nettyserver.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * Created by XMclassmate on 2017/12/18.
 */

public class DiscardServerHandler extends SimpleChannelInboundHandler<Message> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
        try {
                System.out.println("nettyServer:"+new String(msg.getBytes()));
                System.out.flush();
        }finally {
            byte[] bs = "感谢您的来信".getBytes();
            msg.setType((byte) 2);
            msg.setSize(bs.length);
            msg.setBytes(bs);
            ctx.writeAndFlush(msg);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
